<?php

namespace App\Kuafu\CustomPayment\Controller\B;

use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\CustomPayment\Exceptions\CustomPaymentException;
use App\Kuafu\CustomPayment\Service\CustomPaymentService;
use App\Kuafu\CustomPayment\Validate\CustomPaymentShopMappingsValidate;
use App\Kuafu\CustomPayment\Validate\CustomPaymentValidate;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;

class CustomPaymentController extends BaseController
{
    /**
     * 添加自定义支付方式
     *
     * @param Request $request
     * @return JsonResponse
     * @throws CustomPaymentException
     */
    public function add(Request $request): JsonResponse
    {
        list($errMsg, $requestParams) = CustomPaymentValidate::insertValidate($request->all());
        if ($errMsg) {
            return $this->fail(BaseCodeEnum::ERR_400000, $errMsg);
        }
        CustomPaymentService::add($requestParams, auth()->id());
        return $this->success();
    }

    /**
     * 启用 ｜ 停用 支付方式
     *
     * @param Request $request
     * @return JsonResponse
     * @throws CustomPaymentException
     */
    public function setStatus(Request $request): JsonResponse
    {
        list($errMsg, $requestParams) = CustomPaymentValidate::statusValidate($request->all());
        if ($errMsg) {
            return $this->fail(BaseCodeEnum::ERR_400000, $errMsg);
        }
        CustomPaymentService::setStatus($requestParams, auth()->id());
        return $this->success();
    }

    /**
     * 获取支付方式列表
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function getPaymentList(Request $request): JsonResponse
    {
        $name   = (string)$request->get('name', '');
        $status = $request->get('status', '');

        return $this->success(CustomPaymentService::getList($name, $status));
    }


    /**
     * 支付方式下发
     *
     * @param Request $request
     * @return JsonResponse
     * @throws CustomPaymentException
     */
    public function sendPayment(Request $request): JsonResponse
    {
        list($errMsg, $requestParams) = CustomPaymentShopMappingsValidate::insertValidate($request->all());
        if ($errMsg) {
            return $this->fail(BaseCodeEnum::ERR_400000, $errMsg);
        }
        CustomPaymentService::sendPayment($requestParams, auth()->id());
        return $this->success();
    }

    /**
     * 启用 ｜ 停用 门店支付方式
     *
     * @param Request $request
     * @return JsonResponse
     * @throws CustomPaymentException
     */
    public function setStatusShopPayment(Request $request): JsonResponse
    {
        list($errMsg, $requestParams) = CustomPaymentShopMappingsValidate::statusValidate($request->all());
        if ($errMsg) {
            return $this->fail(BaseCodeEnum::ERR_400000, $errMsg);
        }
        CustomPaymentService::setStatusShopPayment($requestParams, auth()->id());
        return $this->success();
    }

    /**
     * 门店结账方式
     * @param Request $request
     * @return JsonResponse
     */
    public function shopPaymentList(Request $request): JsonResponse
    {
        $name     = (string)$request->get('name', '');
        $shopId   = (int)$request->get('shopId', '');
        $pageSize = (int)$request->get('pageSize', 20);
        $pageNum  = (int)$request->get('pageNum', 1);

        return $this->success(CustomPaymentService::shopPaymentList($pageSize, $pageNum, $name, $shopId));
    }

    /**
     * 下发记录
     * @param Request $request
     * @return JsonResponse
     */
    public function sendPaymentHistory(Request $request): JsonResponse
    {
        list($errMsg, $requestParams) = CustomPaymentShopMappingsValidate::sendPaymentHistoryValidate($request->all());
        if ($errMsg) {
            return $this->fail(BaseCodeEnum::ERR_400000, $errMsg);
        }

        $name     = (string)$request->get('name', '');
        $shopId   = (int)$request->get('shopId', '');
        $startAt  = CommonService::getStartTime($request->get('startAt', ''));
        $endAt    = CommonService::getEndTime($request->get('endAt', ''));
        $pageSize = (int)$request->get('pageSize', 20);
        $pageNum  = (int)$request->get('pageNum', 1);

        return $this->success(CustomPaymentService::sendPaymentHistory($pageSize, $pageNum, $name, $shopId, $startAt,
            $endAt));
    }

    /**
     * 获取支付方式已下发门店列表
     *
     * @param Request $request
     * @return JsonResponse
     */
    public  function getPaymentShop(Request $request): JsonResponse
    {
        list($errMsg, $requestParams) = CustomPaymentShopMappingsValidate::paymentShopValidate($request->all());
        if ($errMsg) {
            return $this->fail(BaseCodeEnum::ERR_400000, $errMsg);
        }
        return $this->success(CustomPaymentService::getPaymentShop($requestParams));
    }
}
